//
//  SmallDecisionsZxhOrDmxViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsZxhOrDmxViewController.h"
#import "SmallDecisionsZxhOrDmxInsertPeopleViewController.h"
#import "SmallDecisionsZxhOrDmxSelecModelViewController.h"
#import "SmallDecisionsZxhOrDmxComplateViewController.h"
#import "SmallDecisionsPlateView.h"
#import "SmallDecisionsSingletonHelper.h"

@interface SmallDecisionsZxhOrDmxViewController ()<SmallDecisionsPlateViewDelegate>
@property (nonatomic, copy) NSString *userSelectModelType;
@property (nonatomic, strong) SmallDecisionsPlateView *plateView;
@property (nonatomic, strong) NSArray *names;
@end

@implementation SmallDecisionsZxhOrDmxViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *userPath = [documentPath stringByAppendingPathComponent:@"ZxhOrDmxTypeNames.plist"];
    if ([fileManager fileExistsAtPath:userPath]) {
        NSArray *data = [NSArray arrayWithContentsOfFile:userPath];
        self.plateView.data = data;
        self.names = data;
        [self.plateView startPlateDraw];
    } else {
        self.plateView.data = @[@"1", @"2", @"3", @"4"];
        self.names = @[];
        [self.plateView startPlateDraw];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getUserSelectLocalModelType];
    [self plateView];
    [self.view layoutIfNeeded];
}

- (void)getUserSelectLocalModelType {
    NSString *userSelectModelType = [[NSUserDefaults standardUserDefaults] valueForKey:@"ZxhOrDmxType"];
    if(userSelectModelType) {
        self.userSelectModelType = userSelectModelType;
    } else {
        self.userSelectModelType = @"温和模式";
    }
}


- (SmallDecisionsPlateView *)plateView {
    if(!_plateView) {
        _plateView = [[SmallDecisionsPlateView alloc] init];
        _plateView.delegate = self;
        [self.view addSubview:_plateView];
        [_plateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.view);
            make.height.mas_equalTo(_plateView.mas_width);
        }];
    }
    
    return _plateView;
}

- (void)setUserSelectModelType:(NSString *)userSelectModelType {
    _userSelectModelType = userSelectModelType;
    UIButton *centerView = (UIButton *)self.navigationItem.titleView;
    [centerView setTitle:userSelectModelType forState:UIControlStateNormal];
}


- (UIView *)setCenterBarView {
    AlignButton *centerBtn = [AlignButton buttonWithType:UIButtonTypeCustom];
    centerBtn.alignType = AlignType_TextLeft;
    centerBtn.padding = 5;
    [centerBtn setImage:[UIImage imageNamed:@"global_arrow_down"] forState:UIControlStateNormal];
    [centerBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    [centerBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    centerBtn.titleLabel.font = SmallDecisionsSetFontSize(18);
    centerBtn.frame = CGRectMake(0, 0, 120, 44);
    return centerBtn;
}

- (void)centerViewAction:(UIView *)view {
    weakSelf(self);
    SmallDecisionsZxhOrDmxSelecModelViewController *selecModelVC = [[SmallDecisionsZxhOrDmxSelecModelViewController alloc] init];
    selecModelVC.callback = ^(NSString *userSelectModelType) {
        weakself.userSelectModelType = userSelectModelType;
    };
    [SmallDecisionsNavigatorManager presentViewController:selecModelVC animationType:SmallDecisionsAnimationTypePicker height:370];
}

- (UIView *)setRightBarButton {
    UIButton *addItemsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addItemsBtn setTitle:@"添加玩家" forState:UIControlStateNormal];
    addItemsBtn.titleLabel.font = SmallDecisionsSetFontSize(13);
    [addItemsBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    addItemsBtn.frame = CGRectMake(0, 0, 30, 30);
    return addItemsBtn;
}

- (void)rightBarButtonAction:(UIButton *)view {
    SmallDecisionsZxhOrDmxInsertPeopleViewController *insertPeopleVC = [[SmallDecisionsZxhOrDmxInsertPeopleViewController alloc] init];
    insertPeopleVC.list = self.names;
    weakSelf(self);
    insertPeopleVC.callback = ^(NSArray *peoples) {
        NSMutableArray *names = [NSMutableArray array];
        for (NSString *name in peoples) {
            if (name.length > 0) {
                [names addObject:name];
            }
        }
        if (names.count > 0) {
            NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *namePath = [documentPath stringByAppendingPathComponent:@"ZxhOrDmxTypeNames.plist"];
            NSData *data = [names plistData];
            [data writeToFile:namePath atomically:YES];
            weakself.plateView.data = names.copy;
            weakself.names  = names.copy;
            [weakself.plateView startPlateDraw];
        }
    };
    [SmallDecisionsNavigatorManager presentViewController:insertPeopleVC showNavigationBar:NO animationType:SmallDecisionsAnimationTypeAlert];
}


- (void)plateViewDidFinish:(SmallDecisionsPlateView *)plateView target:(NSInteger)target {
    if([self.userSelectModelType isEqualToString:@"热辣模式"]
       || [self.userSelectModelType isEqualToString:@"混合模式"]) {
        if(![SmallDecisionsSingletonHelper shareInstance].isVIP) {
            [SmallDecisionsSingletonHelper presentVipViewController];
            return;
        }
    }
    [self chooseZxhOrDmxWithIndex:target];
}


- (void)chooseZxhOrDmxWithIndex:(NSInteger)index{
    NSString *name = self.plateView.data[index];
    [SmallDecisionsAlertViewController showAlertWithTitle:name message:@"请选择游戏方式" cancelText:@"真心话" otherText:@"大冒险" dismissCompletion:^(SMALL_DECISIONS_PROJECT_ALERT_HANDLE_EVENTS event) {
        SmallDecisionsZxhOrDmxComplateViewController * complateVC = [[SmallDecisionsZxhOrDmxComplateViewController alloc] init];
        complateVC.userSelectModelType = self.userSelectModelType;
        if(event == SMALL_DECISIONS_PROJECT_ALERT_HANDLE_EVENTS_CANCEL) {
            complateVC.title = @"真心话";
        } else {
            complateVC.title = @"大冒险";
        }
        [SmallDecisionsNavigatorManager presentViewController:complateVC showNavigationBar:NO animationType:SmallDecisionsAnimationTypeAlert];
    }];
}



@end
